# install packages from CRAN
p_needed <- c("tidyverse", "haven",
              "magrittr", "parallel",
              "janitor", "broom", # clean up stuff
              "readxl", "writexl", "png", #  import/export
              "stargazer", "xtable", "summarytools", "tab", "texreg", # format stuff
              "ggthemes", "hrbrthemes",  "RColorBrewer", "networkD3", "grid", "gridExtra", "lemon", # plot stuff
              "margins", "AER", "mokken", # stats stuff
              "estimatr", # for IVrobust regression
              "mediation", # for mediation analyses
              "psych", # for PCA
              "lubridate", "reshape2", "gdata", "scales", "labelled", "xts", "urltools", # work with data
              "metafor", # meta-analysis tools
              "ivdesc" # profiling compliers following Marbach/Hangartner 2019
)
packages <- rownames(installed.packages())
p_to_install <- p_needed[!(p_needed %in% packages)]
if (length(p_to_install) > 0) {
  install.packages(p_to_install)
}
lapply(p_needed, require, character.only = TRUE)

#install.packages("reshape")
#install.packages("coefplot2",repos="http://www.math.mcmaster.ca/bolker/R",type="source")
library(coefplot2)


recode_partynames <- function(x, longnames = FALSE) {
  require(stringr)
  x_recoded <- x %>% str_replace("cdu", "Union") %>%
    str_replace("fdp", "FDP") %>% 
    str_replace("spd", "SPD") %>%
    str_replace("gru", "Grüne") %>%
    str_replace("lin", "Die Linke") %>%
    str_replace("afd", "AfD") %>%
    str_replace("oth", "Andere")
  if(longnames == TRUE) {
    x_recoded <- x_recoded %>% str_replace("Grüne", "B'90/Die Grünen") %>% str_replace("Union", "CDU/CSU") %>% str_replace("Linke", "Die Linke")
  }
  x_recoded
}

repair_partynames <- function(x) {
  require(stringr)
  x_recoded <- x %>% str_replace("B\\?ndnis 90/Die Gr\\?nen", "Greens") %>%
    str_replace("Die Linke", "Left") %>% 
    str_replace("Andere Partei", "Other") %>%
    str_replace("Wei\\? ich noch nicht", "DK")
  x_recoded
}

colors_parties_df <- data.frame(parties = c("CDU/CSU", "SPD", "Left", "Greens", "FDP", "AfD", "Other"), colors = c("#000000", "#EB001F", "#BE3075", "#64A12D", "#FFED00", "#009EE0", "#D3D3D3"), stringsAsFactors = FALSE)

## http://www.cookbook-r.com/Manipulating_data/Summarizing_data/
## Summarizes data.
## Gives count, mean, standard deviation, standard error of the mean, and confidence interval (default 95%).
##   data: a data frame.
##   measurevar: the name of a column that contains the variable to be summariezed
##   groupvars: a vector containing names of columns that contain grouping variables
##   na.rm: a boolean that indicates whether to ignore NA's
##   conf.interval: the percent range of the confidence interval (default is 95%)
summarySE <- function(data=NULL, measurevar, groupvars=NULL, na.rm=FALSE,
                      conf.interval=.95, .drop=TRUE) {
  require(plyr)
  
  # New version of length which can handle NA's: if na.rm==T, don't count them
  length2 <- function (x, na.rm=FALSE) {
    if (na.rm) sum(!is.na(x))
    else       length(x)
  }
  
  # This does the summary. For each group's data frame, return a vector with
  # N, mean, and sd
  datac <- ddply(data, groupvars, .drop=.drop,
                 .fun = function(xx, col) {
                   c(N    = length2(xx[[col]], na.rm=na.rm),
                     mean = mean   (xx[[col]], na.rm=na.rm),
                     sd   = sd     (xx[[col]], na.rm=na.rm)
                   )
                 },
                 measurevar
  )
  
  # Rename the "mean" column    
  datac <- rename(datac, c("mean" = measurevar))
  
  datac$se <- datac$sd / sqrt(datac$N)  # Calculate standard error of the mean
  
  # Confidence interval multiplier for standard error
  # Calculate t-statistic for confidence interval: 
  # e.g., if conf.interval is .95, use .975 (above/below), and use df=N-1
  ciMult <- qt(conf.interval/2 + .5, datac$N-1)
  datac$ci <- datac$se * ciMult
  
  return(datac)
}

# function to get factor order right
setFactorOrder <- function(x, order=sort(levels(x))) { 
  # Returns a factor ordered by `order`.  
  # If order is missing, defaults to `levels(x)` if available, else to `sort(unique(x))`
  # Useful for ggplot and elsewhere were ordering is based on the order of the levels
  
  if (!is.factor(x)) {
    warning("`x` is not a factor. Will coerce.")
    levs <- sort(unique(x))
    if (missing(order))
      order <- levs
  } else {
    levs <- levels(x)
  }
  
  # any values in order, not in levels(x)
  NotInx <- setdiff(order, levs)
  
  if (length(NotInx)) {
    warning ("Some values not in x:\n", paste(NotInx, collapse=", "))
  }
  
  # levels(x) not explicitly named in order
  Remaining <-  setdiff(levs, order)
  
  order <- c(setdiff(order, NotInx), Remaining)
  
  factor(x, level=order)
}

# compute CI bound
compute_ci <- function(dat, z.score) {
  ci <- z.score*sqrt((apply(dat, 2, sd, na.rm = T))/ apply(dat, 2, function(x) length(x[!is.na(x)])))
  ci
}

firstup <- function(x) {
  substr(x, 1, 1) <- toupper(substr(x, 1, 1))
  x
}